<!--组件-现货运行列表-->
<template>
  <div>
    <div class="bg bg2 bk1" v-if="isShow">
      <LImage class="texture3" :src="Config.img('texture3.png')"></LImage>

      <!--服务器标签-->
      <div class="server-tag bg3">
        <text class="font20 text2">{{item.serverName}}</text>
      </div>

      <!--基础信息-->
      <div class="base">

        <div class="column">

          <div class="row aend">
            <text class="font32 text1">{{item.followName ? T(item.followName) : T(item.strategyName)}}</text>
            <text class="font24 text3">{{getStategyType(item)}}</text>

            <div class="row acenter"  v-if="item.errorInfo" @click="msgClick(item)">
              <LImage class='msg' :src="Config.img('baocuo.png')"></LImage>
              <text class="font24 udRed-t1">{{T('信息')}}</text>
            </div>
          </div>

          <div class="height14"></div>
          <text class="font20 text3">{{amRun.getParseTime(item.startTime) + '（' + getExecuteTime(item.startTime, item.endTime) + '）'}}</text>
        </div>
        
        <div class="all"></div>

        <text class="font28 main-t1" v-if="getRunState(item) == T('跟单中')">{{getRunState(item)}}</text>



        <div v-if="item.states=='EXECUTE' && openSignal && item.signalStates==0" class="row acenter">
          <LImage class='msg' :src="Config.img('dengdai.png')"></LImage>
          <text class="font24 udRed-t1">{{T('等待信号触发')}}</text>
        </div>
        <text v-else :class="['font28',  (item.states == 'EXECUTE' ? 'main-t1' : 'text3')]">{{getRunState(item)}}</text>
      </div>
      
      <!--交易所名称-->
      <div class="line bg3"></div>
      <text class="font24 text2">{{getExchangeName(item)}}</text>

      <!--投入信息-->
      <div class="height20"></div>
      <div class="invest">
        <div class="row">
          <text class="font24 text3">{{T('总投入')+'('+item.investCoin+')'}}</text>
          <div class="all"></div>
          <text class="font24 text3">{{T('总收益')+'('+item.investCoin+')'}}</text>
        </div>

        <div class="height8"></div>
        <div class="row">
          <text class="font32 text1">{{item.investCoinnNum}}</text>
          <div class="all"></div>
          <text :class="['font32', Config.upDownFont(item.realEarnings)]">{{item.realEarnings + '(' + item.realEarningsRate + '%)'}}</text>
        </div>
      </div>

      <!--运行信息-->
      <div class="line bg3"></div>
      <div class="run">

        <div class="run-left">
          <text class="font24 text3">{{T('实现盈亏')+'('+item.investCoin+')'}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.earnings)]">{{getAddStr(item.earnings)}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.earnings)]">{{getAddStr(item.earningsRate)+'%'}}</text>
        </div>

        <div class="run-left">
          <text class="font24 text3 ">{{T('浮动盈亏')+'('+item.investCoin+')'}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.floatEarnings)]">{{getAddStr(item.floatEarnings)}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.floatEarnings)]">{{getAddStr(item.floatEarningsRate)+'%'}}</text>
        </div>

        <div class="run-right">
          <text class="font24 text3">{{T('实现年化')+'/'+T('总年化')}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.yearIncome)]">{{getAddStr(item.yearIncome)+'%'}}</text>

          <div class="height8"></div>
          <text :class="['font24', Config.upDownFont(item.realYearIncome)]">{{getAddStr(item.realYearIncome)+'%'}}</text>
        </div>
      </div>

      <!--按钮-->
      <div class="but">

        <div class="tag bg3" v-if="getTag(item).length">
          <text class="font24 text3">{{getTag(item)}}</text>
        </div>

        <div class="all"></div>
        <div class="row acenter" v-if="item.operateStatus == 'moving'">
          <LImage class="time-image" :src="Config.img('bushu_time.png')"></LImage>
          <text class="font30 main-t1">{{T('策略迁移中...')}}</text>
        </div>
        <div class="row acenter" v-else>
          <div class="but-row" v-if="item.strategyName=='DCA智能策略'" 
          @click="pcClick(item)"><text class="font24 main-t1">{{T('一键平仓')}}</text></div>
          <div class="but-row" @click="stopClick(item, index)"><text class="font24 main-t1">{{T('终止')}}</text></div>
          <div class="but-row" @click="shareClick(item)"><text class="font24 main-t1">{{T('分享')}}</text></div>
          <div class="but-row" @click="detailClick(item)"><text class="font24 main-t1">{{T('详情')}}</text></div>
        </div>
      </div>
    </div> 
  </div>
</template>
    
<script>

import math from "@/_js/math";
import modal from "@/_js/modal";
import Config from "@/_js/config";
import amStrategy2 from "@/strategy/amStrategy2";
import broadcast from "@/_js/broadcast";
import navigator from "@/_js/navigator";
import amRun from "@/run/amRun";
import vlink from "@/_js/vlink";

const weexFunction = weex.requireModule("WeexFunction");
import weexHttp from "@/_js/https"; ;

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  props: {
    item: Object, 
  },

  data() {
    return {
      T: T,
      Config: Config,
      amRun: amRun,
      isShow:false,
    };
  },

  mounted() {

    Config.init(()=>{
      this.isShow = true
    },[Config.OPT_UP_DOWN]);
  },

  computed: {
    openSignal() {
      try {
        var config = this.item.config;
        return config.signal.openSignal;
      } catch (error) {
        return false;
      }
    }
  },
  methods: {
    /*********************** 获取数据*/
    //获取策略类型-商城、平台、私有、
    getStategyType(item){

      var stategyType = '';
      if (item.strategyCode) {
        stategyType = item.strategyCode;
      } else if (item.executeType == 'FATHER' || item.executeType == 'OFFICIAL') {
        stategyType = T('平台');
      } else  {
        stategyType = T('私有');
      }
      return '（' + stategyType + '）';
    },

    //获取运行状态
    getRunState(item) {
      var state = amRun.getRunState(false, item.isFollow, item.states);
      return T(state);
    },

    //获交易所名称
    getExchangeName(item){
      var remark = item.remark && item.remark.length ? ('·' + item.remark) : '';
      return item.executeType == 'FATHER' ? 
            (item.apiName + remark + '（' + T('多币对') + ' ' + item.childCount + '）'): 
            (item.apiName + remark + '（' + item.counterCoin + '/' + item.baseCoin + '）');
    },

    //获取标签-活动、跟单、开放跟单
    getTag(item){

      var tag = '';
      var addStr = (tag, str)=>{
        var sign = tag.length ? '、' : '';
        tag = tag + sign + str;
        return tag;
      }

      if (item.activityId != undefined) {
        tag = addStr(tag, T('活动'));
      }
      
      if (item.isFollow) {
        tag = addStr(tag, T('跟单'));
      }

      if (item.isOpenFollow) {
        tag = addStr(tag, T('开放跟单'));
      }

      return tag;
    },

    //获取运行时间长
    getExecuteTime(startTime, endTime){
      var time = amRun.getRunTime(startTime, endTime, T('天'), T('时'), T('分'));
      return time;
    },

    /********************************** http*/
    //http一键平仓
    httpClosePosition(item) {
      modal.showLoad();
      var param = { 'executeId': item.executeId};
      weexHttp.postX("manualStrategy/closePosition",param,true,(data) => {

          modal.hideLoad();
          if (data.status == 200) {
            modal.toast(T('平仓成功'));
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },

    /********************************** Get方法*/
    //获取+字符串
    getAddStr(value) {
      value = math.xiaoShuDelete0(value);
      value = math.addJia(value);
      return value;
    },

    /********************************** 点击触发*/
    //终止点击
    stopClick(item){

      if (item.states == "STOPING" ) {
        modal.toast('请勿频繁操作');
        return;
      }

      var config = item.config;
      var param = {'cancelOrder':config.cancelOrder,
                    'isConvert':config.isConvert}; 
      navigator.openDialog('run/RunStop', param);

      broadcast.subscribeOnce('RunStop', (map)=>{
        
        var isMany = item.executeType == 'FATHER';
        var pair = config.tradePairs[0];
        var apiId = pair.apiId;
        var param = {'executeId':item.executeId, 
                    'apiId':apiId, 
                    'cancelOrder':map.cancelOrder, 
                    'isConvert':map.isConvert,
                    'isMany':isMany};
        amStrategy2.httpEnd(param,()=>{
          broadcast.send(broadcast.EXECUTE_UPDATE);
        });
      });
      
    },

    //消息点击
    msgClick(item){

      if(item.executeType == 'FATHER') {
        modal.toast('请到子币对详情查看');
      } else {
        item.errorInfo = false;
        navigator.open(T('策略消息'), 'strategy/StrategyNotice', {'stId':item.executeId});
      }
    },

    //分享点击
    shareClick(item){

      //跟单
      if (item.isFollow) { 

        vlink.getFollw(item.followExecuteId, (vcode, vtip)=>{

          var tiem = this.getExecuteTime(item.startTime, item.endTime);
          var param = {'nickName':item.nickName,
                      'investCoin':item.investCoin,
                      'totalRate':item.realEarningsRate,
                      'yearRate':item.realYearIncome,
                      'time':tiem};

          param = {'param':param, 
                    'vcode':vcode,
                    'vtip':vtip};
          weexFunction.shareWeex('share/ShareFollw',param);
        });
        return;
      }


      //发布跟单
      var tiem = this.getExecuteTime(item.startTime, item.endTime);
      var pair = item.executeType == 'FATHER' ? (T('多币对')+'('+item.childCount+')')
                                              : (item.counterCoin+'/'+item.baseCoin);
      var param = {'name':item.followName ? item.followName : item.strategyName,
                  'type':item.executeType,
                  'code':item.strategyCode,
                  'totalRate':item.realEarningsRate,
                  'yearRate':item.realYearIncome,
                  'time':tiem,
                  'pair':pair};

      if (item.isOpenFollow) {
        vlink.getFollw(item.executeId, (vcode, vtip)=>{
          param = {'param':param, 
                    'vcode':vcode,
                    'vtip':vtip};
          weexFunction.shareWeex('share/ShareRun',param);
        });
      } else {
        weexFunction.shareWeex('share/ShareRun',{'param':param});
      }
    },

    //详情点击
    detailClick(item){

      if (item.states == "STARTING" ) {
        modal.toast('启动中，无法查看详情！');
        return;
      }

      if(item.executeType == 'FATHER') {
        var param = {'executeId':item.executeId};
        navigator.open('','run/RunInfos',param);
      } else {
        var param = {'executeId':item.executeId};
        navigator.open('','run/RunInfo',param);
      }
      
    },

    //一键平仓
    pcClick(item) {


      var parm = {title:T("一键平仓"),
                  message:T("确认平仓后，现有仓位将按照市价执行卖出操作，但策略不会终止，继续等待新的执行信号。"),
                  okBtn:T("确认"), cancelBtn:T("取消")};
      modal.confirm(parm, (bool)=>{
        if (bool == "true") {
          this.httpClosePosition(item);
        } else {

        }
      });
    }
  },
};
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.msg {
  width: 28px;
  height: 28px;
  margin-right: 6px;
}
.bg {
  border-width: 1px;
  border-radius: 8px;

  padding-top: 40px;
  padding-left: 40px;
  padding-right: 40px;
  padding-bottom: 40px;

  margin-bottom: 20px;
}
.line {
  height: 1px;
  margin-top: 20px;
  margin-bottom: 20px;
}

/*********************** 服务器标题*/
.server-tag {
  position: absolute;
  flex-direction: row;
  align-items: center;
  height: 32px;

  border-bottom-left-radius: 8px;
  border-top-right-radius: 8px;

  padding-left: 14px;
  padding-right: 14px;

  top: 0;
  right: 0;
}

/*********************** 基础信息*/
.base {
  flex-direction: row;
  align-items: center;
}

/*********************** 投入信息*/
.invest {
  flex-direction: column;
  justify-content: space-between;
}

/*********************** 运行信息*/
.run {
  flex-direction: row;
  justify-content: space-between;
}
.run-left{
  flex-direction: column;
}
.run-right {
  flex-direction: column;
  align-items: flex-end;
}

/*********************** 操作按钮*/
.but{
  flex-direction: row;

  margin-top: 40px;
  margin-left: -40px;
  margin-right: -20px;
}
.but-row{
  flex-direction: row;
  align-items: center;
  padding-left: 20px;
  padding-right: 20px;
}
.time-image {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

/*********************** 类型标签*/
.tag{
  flex-direction: row;
  align-items: center;
  height: 64px;
  padding-left: 40px;
  padding-right: 50px;
  border-top-right-radius: 32px;
  border-bottom-right-radius: 32px;
}

/*********************** 间距*/
.height8{
  height: 8px;
}
.height14 {
  height: 14px;
}
.height20{
  height: 20px;
}
</style>